package com.bailugansan.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.bailugansan.pojo.Role;
import com.bailugansan.pojo.User;
import com.bailugansan.service.UserService;
import com.bailugansan.service.implement.UserServiceImpl;
import com.bailugansan.util.Constants;
import com.bailugansan.util.PageSupport;
import com.mysql.jdbc.StringUtils;
import org.junit.jupiter.api.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author BailuGansan
 */
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if(!StringUtils.isNullOrEmpty(method) && "query".equals(method)){
            getUserList(req, resp);
        }

        /**
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);

        User user = (User)o;

        UserService userService = new UserServiceImpl();
        PageSupport pageSupport = new PageSupport();

        // 获取用户总数
        int userCount = userService.getUserCount(null, 0);

        // 使用 PageSupport 设置总页数和页面容量
        pageSupport.setTotalCount(userCount);
        pageSupport.setPageSize(5);*/


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    // 请求转发到用户管理页面
    protected void getUserList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        PageSupport pageSupport = new PageSupport();
        Map<String, PageSupport> param = new HashMap<>();

        // 获取请求中的参数
        String queryname = req.getParameter("queryname");
        String queryUserRoleStr = req.getParameter("queryUserRole");
        String pageIndexStr = req.getParameter("pageIndex");

        // 将角色id 和当前页面索引转换为整形
        int queryUserRole = StringUtils.isNullOrEmpty(queryUserRoleStr)? 0: Integer.parseInt(queryUserRoleStr);
        int pageIndex = StringUtils.isNullOrEmpty(pageIndexStr)? 0: Integer.parseInt(pageIndexStr);

        // 获取用户总数
        int userCount = userService.getUserCount(queryname, queryUserRole);

        // 使用 PageSupport 设置总页数和页面容量
        pageSupport.setCurrentPageNo(pageIndex);
        pageSupport.setPageSize(Constants.PAGE_SIZE);
        pageSupport.setTotalCount(userCount);
//        pageSupport.setTotalPageCountByRs();

        int currentPageNo = pageSupport.getCurrentPageNo();
        int pageSize = pageSupport.getPageSize();

        // 获取角色列表
        List<Role> roleList = userService.getRoleList();

        // 获取用户列表
        int queryIndex = (currentPageNo-1) * pageSize;
        List<User> userList = userService.getUserList(queryname, queryUserRole, queryIndex, pageSize);

        req.setAttribute("queryUserName", queryname);
        req.setAttribute("queryUserRole", queryUserRole);
        req.setAttribute("totalPageCount", pageSupport.getTotalPageCount());
        req.setAttribute("totalCount", pageSupport.getTotalCount());
        req.setAttribute("currentPageNo", currentPageNo);
        req.setAttribute("roleList", roleList);
        req.setAttribute("userList", userList);

        req.getRequestDispatcher("/jsp/userlist.jsp").forward(req, resp);
    }

//    @Test
//    void testPageGet(){
//        UserService userService = new UserServiceImpl();
//        PageSupport pageSupport = new PageSupport();
//        Map<String, PageSupport> param = new HashMap<>();
//        // 获取用户总数
//        int userCount = userService.getUserCount(null, 0);
//        System.out.println(userCount);
//
//        // 使用 PageSupport 设置总页数和页面容量
//        pageSupport.setPageSize(5);
//        pageSupport.setTotalCount(userCount);
//
//        System.out.println(pageSupport.getCurrentPageNo());
//        System.out.println(pageSupport.getPageSize());
//        System.out.println(pageSupport.getTotalCount());
//        System.out.println(pageSupport.getTotalPageCount());
//    }

}
